APM.ConfigPositionController = Ember.Controller.extend({
	init : function() {
		this._super();
		this.initConfData();
	},
	
	form: {
		areaCode:"",
		globleCode:"00",
		proCode:"",
		cityCode:"",
		id:""
	},  
	
	initConfData: function(){
		var ctrl = this;
		var form = this.get("form");
		ctrl.store.ajax("GET", "configs/position").then(function(response){
				var maps=response.maps;
				if(maps){
				ctrl.set("maplists",Ember.A(maps));
				}	
			});
		
	},
	
	gloArrays: Ember.computed(function(key, value){
		var ctrl = this;
		if(arguments.length == 1){
			ctrl.store.ajax("GET", "configs/position/area", {
				areaCode:null
			}).then(function(response){
				ctrl.set("gloArrays", Ember.A(response.area));
			});
			return Ember.A();
		}else{
			return value;
		}
	}),

	gloArraysChange: Ember.observer("gloArrays", function(){
		var ctrl = this;
		var gloArrays = ctrl.get("gloArrays");
		if(gloArrays.length>0){
			// console.info("gsize:"+gloArrays.length);
			// console.info("gggg:"+ctrl.get("gloNumCode"));
			if(gloArrays[0].code=="00"){
				ctrl.set("form.globleCode", gloArrays[0].code);
			}
		}
	}),

	globleCodeChange: Ember.observer("form.globleCode", function(){
		var ctrl = this;
		var globleCode = this.get("form.globleCode");
		ctrl.set("form.proCode","");
		if(globleCode){
			ctrl.store.ajax("GET", "configs/position/area", {
				areaCode: globleCode
			}).then(function(response){
				ctrl.set("protlists", Ember.A(response.area));
				ctrl.set("cityNumCode","");
				ctrl.set("form.cityCode","");
			});
		}
	}),
	
	//省列表存放数组
	protlists: Ember.computed(function(key, value){
		if(arguments.length == 1){
			return Ember.A([{
				"name":"省份",
				"code":""
			}]);
		}else{
			value.splice(0, 0,{
				"name":"省份",
				"code":""
			});
			return value;
		}
	}),
	
	proArraysChange: Ember.observer("protlists", function(){
		var ctrl = this;
		var proArrays = ctrl.get("protlists");
		if(proArrays.length>0){
			// console.info("psize:"+proArrays.length);
			// console.info("ppp:"+ctrl.get("proNumCode"));
			if(proArrays.length>1){
				ctrl.set("form.proCode", ctrl.get("proNumCode")||proArrays[0].code);
			}else{
				ctrl.set("form.cityCode","");
				ctrl.set("citylists", Ember.A());
			}
		}
	}),
	
	proCodeChange: Ember.observer("form.proCode", function(){
		var ctrl = this;
		var proCode = this.get("form.proCode");
		ctrl.set("form.cityCode","");
		if(proCode && proCode!="0000" && proCode!="0001" && proCode!="0002" && proCode!="0003"){
			ctrl.store.ajax("GET", "configs/position/area", {
				areaCode: proCode
			}).then(function(response){
				ctrl.set("citylists", Ember.A(response.area));
			});
		}else{
			ctrl.set("cityNumCode","");
			ctrl.set("form.cityCode","");
			ctrl.set("citylists",Ember.A());
		}
	}),
	
	//市列表存放数组
	citylists: Ember.computed(function(key, value){
		if(arguments.length == 1){
			return Ember.A([{
					"name":"市",
					"code":""
			}]);
		}else{
			value.splice(0, 0,{
				"name":"市",
				"code":""
			});
			return value;
		}
	}),
	
	cityArraysChange: Ember.observer("citylists", function(){
		var ctrl = this;
		var cityArrays = ctrl.get("citylists");
		if(cityArrays.length>0){
			// console.info("csize:"+cityArrays.length);
			// console.info("ccc:"+ctrl.get("cityNumCode"));
			if(cityArrays.length>1){
				ctrl.set("form.cityCode", ctrl.get("cityNumCode")||cityArrays[0].code);
			}
		}
	}),
	
	// cityCodeChange: Ember.observer("form.cityCode", function(){
	// 	var ctrl = this;
	// 	var form=ctrl.get("form");
	// 	var cityCode = ctrl.get("form.cityCode");
	// 	ctrl.set("form.disCode","");
	// 	if(cityCode){
	// 		ctrl.store.ajax("GET", "configs/position/area", {
	// 			areaCode: cityCode
	// 		}).then(function(response){
	// 			ctrl.set("distlists", Ember.A(response.area));
	// 		});
	// 	}else{
	// 		ctrl.set("disNumCode","");
	// 		ctrl.set("form.disCode","");
	// 		ctrl.set("distlists",Ember.A());
	// 	}
	// }),
	
	// //区列表存放数组
	// distlists: Ember.computed(function(key, value){
	// 	if(arguments.length == 1){
	// 		return Ember.A([{
	// 			"name":"区",
	// 			"code":""         
	// 		}]);
	// 	}else{
	// 		value.splice(0, 0,{
	// 			"name":"区",
	// 			"code":""
	// 		});
	// 		return value;
	// 	}
	// }),
	
	// disArraysChange: Ember.observer("distlists", function(){
	// 	var ctrl = this;
	// 	var disArrays = ctrl.get("distlists");
	// 	if(disArrays.length>0){
	// 		console.info("dsize:"+disArrays.length);
	// 		console.info("ddd:"+ctrl.get("disNumCode"));
	// 		ctrl.set("form.disCode", ctrl.get("disNumCode")||disArrays[0].code);
	// 	}
	// }),
		
	//地图列表存放数组
	maplists : Ember.computed(function(key, value){
		if(arguments.length == 1){
			return Ember.A();
		}else{
			return value;
		}
	}),

	actions: {
		reset : function() {
			this.set("formTip", null);
			this.set("saveResult", null);
		},
		
		//查询地图列表
         queryMaps:function(){
        	 var ctrl = this;
 				this.store.ajax("GET", "configs/position").then(function(response){
	 				var maps=response.maps;
	 				if(maps){
						ctrl.set("maplists",Ember.A(maps));
	 				}
	 			});
         },
         
         //设置位置
		setArea: function(){
			var ctrl=this;
			var form=this.get("form");
			var id=form.id;
			var areaCode="";
			if(form.globleCode){
				areaCode=ctrl.get("form.globleCode");
			}
			if(form.proCode){
				areaCode=ctrl.get("form.proCode");
			}
			if(form.cityCode){
				areaCode=ctrl.get("form.cityCode");
			}
			
			// if(form.disCode){
			// 	areaCode=ctrl.get("form.disCode");
			// }
			
//			console.log("proCode:"+form.proCode+"\tcityCode:"+form.cityCode+"\tdisCode:"+form.disCode);
//			alert("areaCode:"+areaCode);
			ctrl.store.ajax("GET", "configs/position/"+id, {
					mapId:areaCode
			}).then(function(response){
				if(response.status=="success"){
					//todo: 保存成功逻辑
					ctrl.set("saveResult", "设置成功！");
				}else{
					ctrl.set("saveResult", response.message);
				}
				ctrl.send("queryMaps");
			}, function(){
				ctrl.set("saveResult", "设置失败！");
			});
			ctrl.set("citylists", Ember.A());
			// ctrl.set("distlists", Ember.A());
			ctrl.set("proNumCode","");
			ctrl.set("cityNumCode","");
			// ctrl.set("disNumCode","");
			ctrl.set("form.proCode","");
			ctrl.set("form.cityCode","");
			// ctrl.set("form.disCode","");
			ctrl.set("form.areaCode",null);
			ctrl.send("closeDialog");
		},
         
         //省市区三级查询
 		queryArea: function(areaCode){
 			var ctrl=this;
 			var form = this.get("form");
 			ctrl.store.ajax("GET", "configs/position/area", {
 				areaCode:form.areaCode
 			}).then(function(response){
 				areas=response.area;
 				ctrl.set("prolists",areas);
 			}, function(){
 				ctrl.set("saveResult", "查询地址失败！");
 			});
 		},
		
		//对话框操作方法
		 openDialog: function(obj){
			 	var ctrl=this;
	 			var form = this.get("form");
	 			ctrl.set("saveResult", null);
				ctrl.set("citylists", Ember.A());
				// ctrl.set("distlists", Ember.A());
				ctrl.set("proNumCode","");
				ctrl.set("cityNumCode","");
				// ctrl.set("disNumCode","");
				ctrl.set("form.proCode","");
				ctrl.set("form.cityCode","");
				// ctrl.set("form.disCode","");
			 	var areaObj=obj.area;
			 	ctrl.set("form.id", obj.appId);
//			 	gloNumCode  proNumCode  cityNumCode  disNumCode
			 	if(areaObj){
			 		var objsize=areaObj.length;
					if(objsize>0){
						ctrl.set("gloNumCode",areaObj[0].code);
					}
					if(objsize>1){	
				 	ctrl.set("proNumCode",areaObj[1].code);
					}
					if(objsize>2){
				 	ctrl.set("cityNumCode",areaObj[2].code);
					}
					// if(objsize>3){
				 // 	ctrl.set("disNumCode",areaObj[3].code);
					// }
					ctrl.set("form.globleCode",areaObj[0].code);
					if(objsize>1){
						ctrl.set("form.proCode",areaObj[1].code);
					}
			 	}
		        var dialog = Broada.getView("position_map_set");
		        Broada.getView("position_map_set").openDialog();
		 },
		
		 closeDialog: function(){
				Broada.getView("position_map_set").closeDialog();
		 }
	}
});